<?php
	
	date_default_timezone_set('Europe/Rome');

	if(isset($_POST['sAPP']) && !empty($_POST['sAPP'])) {
		$sAPP = $_POST['sAPP'];	
	} else {
		die('ERROR: App not defined');
	}

	require_once "../$sAPP/include/const.inc.php";
	require_once "../$sAPP/include/functions.php";
	require_once "auth.inc.php";
	require_once "conn.inc.php";

	fnGetValueFromPOST();

	$sScript = fnCurrentScript();
	$iAccessLvl = fnGetAccessLvl('app');

	if (is_allowed('edit', $iAccessLvl)) {
			
		$sQuery = "SELECT * FROM `resources` WHERE `id` = '".$iResource."'";
		$rResult = custom_mysql_query($sQuery);

		if (mysql_num_rows($rResult) == 1) {
		
			if ($sField == 'sDescription') {
				
				/**
				 * TABELLA `resources`
				 */
				
				if ($sValue != '') {
					$sQuery = "UPDATE `resources` SET `$sField` = '$sValue', `tsLastModify` = CURRENT_TIMESTAMP WHERE `id` = '$iResource'";
					$rResult = custom_mysql_query($sQuery);
				}
					
				$sQuery = "SELECT `$sField` FROM `resources` WHERE `id` = '$iResource'";
				$rResult = custom_mysql_query($sQuery);
				
				if (mysql_num_rows($rResult) == 1) {
					
					$aRow = mysql_fetch_assoc($rResult);
					echo $aRow[$sField]; 
					
				} else {
					
					/**
					 * c'è qualcosa che non ha funzionato come avrebbe dovuto...
					 */
					
					fnUpdateLog("NOT FOUND", $iResource, 'resources','E');					
				
				} 
				
				
			} else if (in_array($sField, $aRESOURCES[$sType]['aFields'])) {
				
				/**
				 * TABELLA `properties`
				 */

				$sQuery = "SELECT * FROM `properties` WHERE  `sSelectFrom` = 'resources' AND `iTable` = '$iResource' AND `sField` = '$sField' ";
				$rResult = custom_mysql_query($sQuery);
			
				if (mysql_num_rows($rResult) == 1 && $sValue != '') {
					$sQuery = "UPDATE `properties` SET `sValue` = '$sValue' WHERE  `sSelectFrom` = 'resources' AND `iTable` = '$iResource' AND `sField` = '$sField' ";
				
				} else if (mysql_num_rows($rResult) == 1 && $sValue == '') {
					$sQuery = "DELETE FROM `properties` WHERE  `sSelectFrom` = 'resources' AND `iTable` = '$iResource' AND `sField` = '$sField' ";
				
				} else if (mysql_num_rows($rResult) == 0 && $sValue != '') {
					$sQuery = "INSERT INTO `properties` (`sSelectFrom`, `iTable`, `sField`, `sValue`) VALUES ('resources', '$iResource', '$sField', '$sValue')";
				
				} else {
					$sQuery = '';
				
				}
				
				if (!empty($sQuery)) {
					$rResult = custom_mysql_query($sQuery);
				}
	
				$sQuery = "SELECT `sValue` FROM `properties` WHERE  `sSelectFrom` = 'resources' AND `iTable` = '$iResource' AND `sField` = '$sField' ";
				$rResult = custom_mysql_query($sQuery);
			
				if ($sValue != '' && mysql_num_rows($rResult) == 1) {
				
					$aRow = mysql_fetch_assoc($rResult);
				
					if ($sField == 'iParent') {
				
						$sQuery = "SELECT `sDescription` FROM `resources` WHERE `id` = '$sValue' ";
						$rResult = custom_mysql_query($sQuery);
						
						if (mysql_num_rows($rResult) == 1) {
							$aRow = mysql_fetch_assoc($rResult);
							echo $aRow['sDescription'];
							
						} else {
							
							/**
							 * c'è qualcosa che non ha funzionato come avrebbe dovuto...
							 * non esiste il record relativo a `iParent`
							 */
							
							fnUpdateLog("NOT FOUND", $sValue, 'resources','E');		
									
						}
						
					} else {
						
						echo $aRow['sValue'];
						
					}
				
				} else if ($sValue == '' && mysql_num_rows($rResult) == 0) {
					
					echo '';
				
				} else {
					
					/**
					 * c'è qualcosa che non ha funzionato come avrebbe dovuto...
					 */
					
					fnUpdateLog("NOT FOUND [sSelectFrom: resources, iTable: $iResource, sField: $sField]", 0, 'properties','E');	

				} 
			
			} else {
				
				/**
				 * c'è qualcosa che non ha funzionato come avrebbe dovuto:
				 * $sField non è definito in $aRESOURCES[$sType]['aFields']
				 */
				
				fnUpdateLog("NOT FOUND IN \$aRESOURCES [sType: $sType, sField: $sField]", 0, '','E');						
			
			} 
		
		} else {
			
			/**
			 * c'è qualcosa che non ha funzionato come avrebbe dovuto...
			 */
			
			fnUpdateLog("NOT FOUND", $iResource, 'resources','E');	
					
		}

	} else {
		
		fnUpdateLog('LOW RIGHTS', 0, 'resources','S');
	
	}

?>